package com.wlyuan.open.remote.facade;

import cn.hutool.core.lang.Validator;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.remote.facade.translator.UserTranslator;
import com.wlyuan.user.api.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

/**
 * @author lijing
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserFacade {
    private final IUserService userService;
    private static Pattern NUMBER_PATTERN = Pattern.compile("^-?\\d+(\\.\\d+)?$");

    public User check(String account) {
        var userDetailVO = userService.checkAdminAccount(account, "");
        if (userDetailVO == null) {
            throw new ValueError("账号不存在");
        }
        logger.info("user userDetailVO : {}", userDetailVO);
        return UserTranslator.translate(userDetailVO);
    }

    public void bindAccount(Long userId, String openAccount) {
        userService.bindOpenAccount(userId, openAccount);
    }

    public void unBindAccount(Long userId) {
        userService.unBindOpenAccount(userId);
    }

    public User getById(Long userId) {
        var userDetailVO = userService.getUserById(userId);
        logger.info("user userDetailVO : {}", userDetailVO);
        return UserTranslator.translate(userDetailVO);
    }

    public String getMobile(String account) {
        var user = check(account);
        var isMobile = Validator.isMobile(user.getMobile());
        if (!isMobile) {
            throw new ValueError("手机号格式不正确,不能发送验证码");
        }
        return user.getMobile();
    }
}
